//
//  LKRespondHandler.m
//  Live
//
//  Created by Pie on 2017/7/26.
//  Copyright © 2017年 LaKa. All rights reserved.
//

#import "LKRespondHandler.h"
#import "LKJsonRequestManager.h"
#import "LKRequestLocalCodeHandler.h"
#import <AFNetworking/AFNetworking.h>
#import <AFNetworking/AFURLSessionManager.h>
#import "LKRequestErrorCode.h"
#import "BNDOfficeDocKit.h"


@implementation LKRespondHandler

#pragma mark - Public Method

+ (void)handleRespond:(NSDictionary *)responseObject
              succeed:(LKJsonQuerySuccessBlock)success
               failed:(LKJsonQueryFailureBlock)failure
       sessionManager:(LKJsonRequestManager *)sessionManager
{
    NSError *error = nil;
    //    if (([responseObject[@"errno"] floatValue] ==-5)||([responseObject[@"errno"] floatValue] ==-4)||([responseObject[@"errno"] floatValue] ==501)) {
    ////        [[LKPreference sharedInstance] saveLoginUserToDisk:nil];
    ////        [LKLoginHandler login:nil];
    //        NSString *locationMessage = responseObject[@"errmsg"];
    //        error = [NSError errorWithDomain:@""
    //                                    code:1
    //                                userInfo:@{NSLocalizedDescriptionKey:TTStringNotNil(locationMessage)}];
    //        if(failure) {
    //            failure(error);
    //        }
    //    }else if (([responseObject[@"errno"] floatValue] ==-1)||([responseObject[@"errno"] floatValue] ==-2)||([responseObject[@"errno"] floatValue] ==403)||([responseObject[@"errno"] floatValue] ==401)||([responseObject[@"errno"] floatValue] ==740) ||([responseObject[@"errno"] floatValue] ==402)){
    //        NSString *locationMessage = responseObject[@"errmsg"];
    ////        if([locationMessage isEqualToString:@"用户已被冻结"]){
    ////            [[LKPreference sharedInstance] saveLoginUserToDisk:nil];
    ////            [LKLoginHandler login:nil];
    ////            [ReminderHUD showReminderText:locationMessage delayTime:1];
    ////        }
    //        error = [NSError errorWithDomain:@""
    //                                        code:1
    //                                    userInfo:@{NSLocalizedDescriptionKey:TTStringNotNil(locationMessage)}];
    //        if(failure) {
    //            failure(error);
    //        }
    if (([responseObject[@"code"] floatValue] !=200)) {
        NSString *locationMessage = responseObject[@"errmsg"];
        error = [NSError errorWithDomain:@""
                                    code:1
                                userInfo:@{NSLocalizedDescriptionKey:TTStringNotNil(locationMessage)}];
        if(failure) {
            failure(error);
        }
    }
    else {
        if(success) {
            success(responseObject);
        }
    }
    
    //   [sessionManager invalidateSessionCancelingTasks:NO];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}

+ (void)handleFailed:(NSError *)error
              failed:(LKJsonQueryFailureBlock)failure
      sessionManager:(LKJsonRequestManager *)sessionManager
{
    NSError *localError = [LKRequestLocalCodeHandler localizeErrorWithError:error];
    
    if(localError.code != LK_NETWORK_ERROR_USER_CANCEL) {
        // 如果不是程序主动取消请求，则不回调
        if(failure) {
            failure(localError);
        }
    }
    
    //  [sessionManager invalidateSessionCancelingTasks:NO];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}


/**
 主要是用于处理token错误
 */
+ (void)handleErrorWithCode:(LKRequestErrorType)code
{
    if(code == LK_ERROR_USER_NOT_LOGIN ||
       code == LK_ERROR_USER_NO_EXIT ||
       code == LK_ERROR_TOEKN_EXPIRE ||
       code == LK_ERROR_WRONG_USER_TOEKN)
    {
        //        [[LKPreference sharedInstance] saveLoginUserToDisk:nil];
        //        [LKLoginHandler login:nil];
    }
}

/**
 这个方法是替换服务器返回的错误信息， 用于提示给用户看的
 
 @param code 错误码
 @param serversMessage 服务器返回的信息
 @return 替换之后的提示信息
 */
+ (NSString *)loactionMeesageForCode:(LKRequestErrorType)code serversMessage:(NSString *)serversMessage
{
    
    NSString *locationMessage = serversMessage;
    
    switch (code) {
            
        case LK_ERROR_TOEKN_EXPIRE: {
            locationMessage = @"登录信息过期";
            break;
        }
        case LK_ERROR_WRONG_USER_TOEKN: {
            locationMessage = @"登录信息异常";
            break;
        }
        case LK_ERROR_GET_WECHAT_ACCESS_TOKEN_FAILED: {
            locationMessage = @"获取微信信息失败";
            break;
        }
        case LK_ERROR_GET_WEIBO_ACCESS_TOKEN_FAILED: {
            locationMessage = @"获取微博信息异常";
            break;
        }
        case LK_ERROR_GET_QQ_ACCESS_TOKEN_FAILED: {
            locationMessage = @"获取QQ信息异常";
            break;
        }
            
        default:
            break;
    }
    return locationMessage;
}
@end
